const { secret } = require('../config/config')
const { verify } = require('jsonwebtoken')



class CommonMiddleware  {

  // 检验token是否过期
  async TokenIsTimeout (ctx, next) {
    const token = ctx.header.authorization;
    if (token) {
      try {
       await  verify(token.split(' ')[1], secret);
       await next();
      } catch (err) {
        ctx.body = {
          message: '令牌已过期,请重新登录',
          statusCode: 401
        }
      }
    } else {
      ctx.body = {
        message: '你还未登录,请登录后再操作',
        statusCode: 403
      }
    }
  }

}

module.exports = new CommonMiddleware()